/*
<Cobbled.java> - A representation of the Cobbled Problem.
----------------------------------------------------------------
Copyright(C) 2005 Daniel Estévez Fuentes <dan@ciencias.unam.mx> 

This  program is free  software; you can redistribute it and/or
modify  it  under the terms of the  GNU  General Public License
as published by the  Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the  hope that it will be useful,
but  WITHOUT ANY WARRANTY;  without even the implied warranty of
MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.   See the
GNU General Public License for more details.

You  should  have  received  a  copy of the  GNU  General Public
License  along  with  this  program;  if  not, write to the Free
Software  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA  02111-1307, USA.

-----------------------------------------------------------------
$Log$ 

------------------------------------------------------------------
 */
package practica2.cobbled;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * A representation of the Cobbled Problem.
 * @author   Daniel Estévez Fuentes <dan@ciencias.unam.mx>
 * @version  1.0
 */
public abstract class Cobbled extends JFrame {

    /** Colores. */
    protected Properties properties;
    /** Tablero para pintar la cuadricula.*/
    protected SquaresBoard sboard;

    /**
     * Constructor that receives the dimension's squareds' board.
     * @param dimension int for squares' board length.
     */
    public Cobbled(int dimension) throws IOException {
        lookAndFeel();
        properties = new Properties();
        properties.load(new FileInputStream("properties/config.properties"));
        sboard = new SquaresBoard(dimension,
                getColor(properties.getProperty("colorline")));
        init();
    }

    private void lookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Cobbled.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Cobbled.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Cobbled.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Cobbled.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void init() {
        int sizex = 600, sizey = 600;
        setSize(sizex, sizey);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screen.width / 2 - sizex / 2, screen.height / 2 - sizey / 2);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(sboard, BorderLayout.CENTER);

        JButton breset = new JButton(properties.getProperty("reset"));
        breset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                sboard.resetBoard();
                resetTablero();
            }
        });

        JButton brecur = new JButton(properties.getProperty("recursive"));
        brecur.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                sboard.setItems(toColorMatrix(recursiveCobbled()));
            }
        });

        JButton bitera = new JButton(properties.getProperty("iterative"));
        bitera.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                sboard.setItems(toColorMatrix(iterativeCobbled()));
            }
        });

        JPanel buttons = new JPanel(new GridLayout(3, 1));
        buttons.add(brecur);
        buttons.add(bitera);
        buttons.add(breset);

        getContentPane().add(buttons, BorderLayout.SOUTH);

        setVisible(true);
    }

    private Color getColor(String str) {
        String[] toks = str.split(",");
        return new Color(Integer.parseInt(toks[0]),
                Integer.parseInt(toks[1]),
                Integer.parseInt(toks[2]));
    }

    private Color[][] toColorMatrix(int[][] matrix) {
        Color[][] ret = new Color[sboard.getDimension()][sboard.getDimension()];
        for (int i = 0; i < sboard.getDimension(); i++) {
            for (int j = 0; j < sboard.getDimension(); j++) {
                ret[i][j] = getColor(properties.getProperty("color" + matrix[i][j]));
            }
        }
        return ret;
    }

    //Agregado
    public void resetTablero() {
    }

    /**
     * Implements a recursive Coobled
     */
    public abstract int[][] recursiveCobbled();

    /**
     * Implements a iterative Coobled
     */
    public abstract int[][] iterativeCobbled();
}
